package com.bw.user.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.bw.core.result.ResponseResult;
import com.bw.core.utils.MD5Sign;
import com.bw.core.utils.StringUtils;
import com.bw.core.vo.UserVo;
import com.bw.core.vo.UserVo;
import com.bw.user.pojo.TbUser;
import com.bw.user.service.TbUserService;
import com.bw.user.vo.*;
import org.apache.commons.lang.RandomStringUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author lv
 * @since 2021-03-23
 */
@RestController
@RequestMapping("/tb-user")

public class TbUserController {

    @Autowired
    private TbUserService userservice;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Autowired
    private HttpServletRequest httpServletRequest;

    /**
     * @param loginReqVo
     * @return 普通登录
     */
    @PostMapping("/login")
    public ResponseResult login(@RequestBody LoginReqVo loginReqVo) {

        if (ObjectUtil.isNull(loginReqVo)) {
            return ResponseResult.error("对象为空");
        }
        if (StrUtil.hasEmpty(loginReqVo.getPhone())) {
            return ResponseResult.error("请输入手机号!");
        }
        if (StrUtil.hasEmpty(loginReqVo.getPassword())) {
            return ResponseResult.error("请输入密码!");
        }


        return userservice.findUser(loginReqVo);
    }


    /**
     * @param sendReqVo
     * @return 获取验证码
     */
    @PostMapping("/send")
    public ResponseResult send(@RequestBody SendReqVo sendReqVo) {
        String mini = "asdfghjkl";
        String key = sendReqVo.getPhone() + mini;
        String s = MD5Sign.md5Encode(key);
        System.out.println(s);
        if (!s.equals(sendReqVo.getSign())) {
            return ResponseResult.error("签名不合法");
        }
        return userservice.send(sendReqVo);

    }

    /**
     *
     * @param loginMessage
     * @return
     * 通过账号密码登录
     */
    @PostMapping("/loginpwd")
    public ResponseResult login_pwd(@RequestBody LoginMessage loginMessage) {
        if (StringUtils.isEmpty(loginMessage.getPhone())) {
            return ResponseResult.error("请输入手机号!");
        }
        if (ReUtil.isMatch("^1[34578][0-9]{8}$", loginMessage.getPhone())) {
            return new ResponseResult(ResponseResult.ERROR, "请输入正确的手机号");
        }
        if (StringUtils.isEmpty(loginMessage.getPassword())) {
            return ResponseResult.error("密码不能为空");
        }
        return userservice.loginpwd(loginMessage);
    }

    //通过短信登录
    @PostMapping("/loginmsg")
    public ResponseResult login_msg(@RequestBody LoginMessage loginMessage){

        if (StringUtils.isEmpty(loginMessage.getPhone())) {
            return ResponseResult.error("请输入手机号!");
        }
        if (StrUtil.isEmpty(loginMessage.getYzm())) {
            return new ResponseResult(ResponseResult.ERROR, "验证码不可为空");
        }
        String o = (String) redisTemplate.boundValueOps(loginMessage.getPhone()).get();
        if (StrUtil.isEmpty(o)) {
            return new ResponseResult(ResponseResult.ERROR, "验证码已超时", o);
        }
        if (!loginMessage.getYzm().equals(o)) {
            return new ResponseResult(ResponseResult.ERROR, "验证码错误", o);
        }

        return userservice.loginmsg(loginMessage);

    }

    /**
     * @param registerVo
     * @return 注册
     */
    @PostMapping("/register")
    public ResponseResult register(@RequestBody RegisterVo registerVo) {

        if (ObjectUtil.isNull(registerVo)) {
            return ResponseResult.error("对象不能为空");
        }
        return userservice.register(registerVo);
    }

    /**
     * @param updatepwdVo
     * @return 修改密码
     */
    @PutMapping("/updatepwd")
    public ResponseResult update(@RequestBody UpdatepwdVo updatepwdVo) {

        if (ObjectUtil.isNull(updatepwdVo)){
            return ResponseResult.error("对象为空");
        }
        if (StrUtil.hasEmpty(updatepwdVo.getOldpassword())) {
            return ResponseResult.error("请输入旧密码!");
        }

        if (StrUtil.hasEmpty(updatepwdVo.getNewpassword())) {
            return ResponseResult.error("请输入新密码!");
        }
        //通过手机号获取用户
        TbUser user = userservice.findUser(updatepwdVo.getPhone());

        //对加密的旧密码和用户原密码进行对比
        if (!user.getPassword().equals(updatepwdVo.getOldpassword())) {
            return ResponseResult.error("旧密码不正确!");
        }

        //通过手机号获取redis里的验证码
        String yzm = (String) redisTemplate.opsForValue().get(updatepwdVo.getPhone());

        if (StringUtils.isEmpty(yzm)) {
            return ResponseResult.error("验证码超时!");
        }
        if (!yzm.equals(updatepwdVo.getYzm())) {
            return ResponseResult.error("验证码有误");
        }
        //修改密码
        return userservice.updateUser(user, updatepwdVo.getNewpassword());

    }

    //找回密码
    @PostMapping("/findpwd")
    public ResponseResult find_pwd(@RequestBody LoginMessage loginMessage){

        if (StringUtils.isEmpty(loginMessage.getPhone())) {
            return ResponseResult.error("请输入手机号!");
        }
        if (ReUtil.isMatch("^1[34578][0-9]{8}$", loginMessage.getPhone())) {
            return new ResponseResult(ResponseResult.ERROR, "请输入正确的手机号");
        }
        if (StrUtil.isEmpty(loginMessage.getYzm())) {
            return new ResponseResult(ResponseResult.ERROR, "验证码不可为空");
        }
        String o = (String) redisTemplate.boundValueOps(loginMessage.getPhone()).get();
        if (StrUtil.isEmpty(o)) {
            return new ResponseResult(ResponseResult.ERROR, "验证码已超时", o);
        }
        if (!loginMessage.getYzm().equals(o)) {
            return new ResponseResult(ResponseResult.ERROR, "验证码错误", o);
        }
        if (StringUtils.isEmpty(loginMessage.getPassword())) {
            return ResponseResult.error("密码不能为空");
        }
        if (StringUtils.isEmpty(loginMessage.getNewpassword())) {
            return ResponseResult.error("确认密码不能为空");
        }
        if (!loginMessage.getPassword().equals(loginMessage.getNewpassword())) {
            return ResponseResult.error("密码不一致");
        }
        return userservice.findpwd(loginMessage);
    }




/*    @PostMapping("/identity")
    public ResponseResult identity(@RequestBody IdentityVo identityVo, MultipartFile file)
    {
//        FileItemStream.ItemSkippedException;
//        OSSBootUtil.upload(FileItemStream.ItemSkippedExceptionItemSkippedException,"F:\\file")
        String upload = OSSBootUtil.upload(file, "F:\\file");
        return ResponseResult.success(upload);

    }*/

    //---------风控---------风控--------风控---------风控----------

    @RequestMapping(value = "/findUserMessage", method = RequestMethod.GET)
    public UserVo findUserMessage(@RequestParam("userId") Long userId) {
        if (userId != null && userId > 0) {
            UserVo userVo = userservice.findUserMessage(userId);
            if (ObjectUtil.isNotEmpty(userVo)) {
                return userVo;
            }
            return null;
        }
        return null;
    }


    @RequestMapping(value = "/findCallBook", method = RequestMethod.GET)
    public List<String> findCallBook(@RequestParam("userId") Long userId) {
        if (userId != null && userId > 0) {
            List<String> callBook = userservice.findCallBook(userId);
            if (ObjectUtil.isNotEmpty(callBook)) {
                return callBook;
            }
            return null;
        }
        return null;
    }


    @RequestMapping(value = "/findLinkMan", method = RequestMethod.GET)
    public List<String> findLinkMan(@RequestParam("userId") Long userId) {
        if (userId != null && userId > 0) {
            List<String> linkMan = userservice.findLinkMan(userId);
            if (ObjectUtil.isNotEmpty(linkMan)) {
                return linkMan;
            }
            return null;
        }
        return null;
    }

    @RequestMapping(value = "findTaoBao", method = RequestMethod.GET)
    String findTaoBao(@RequestParam("userId") Long userId) {
        if (userId != null && userId > 0) {
            String taoBao = userservice.findTaoBao(userId);
            if (ObjectUtil.isNotEmpty(taoBao)) {
                return taoBao;
            }
            return null;
        }
        return null;
    }

    @RequestMapping(value = "findOperator", method = RequestMethod.GET)
    String findOperator(@RequestParam("userId") Long userId) {
        if (userId != null && userId > 0) {
            String operator = userservice.findOperator(userId);
            if (ObjectUtil.isNotEmpty(operator)) {
                return operator;
            }
            return null;
        }
        return null;
    }

    //---------风控---------风控--------风控---------风控----------

}


